/*
 * CreepHardGrey.java
 *
 * Created on January 4, 2008, 6:38 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package hextd.creep;

import hextd.core.ContextPlayer;
import hextd.util.Cache;
import hextd.util.Geometry;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;

/**
 *
 * @author hylke
 */
public class CreepHardGrey extends CreepBase {

	public static String type = "hg";
	//
	private int prevPaintTime;
	private Color bodyColor = new Color(150, 150, 150);
	private Stroke bodyStroke;
	private Shape bodyShape;
	private static float reductionFactor = 0.005f;

	/** Creates a new instance of CreepHardGrey */
	public CreepHardGrey() {
		super("Hard Grey");
		this.damageColor = Creep.COLOR.OTHER;
		this.damageColorAnti = Creep.COLOR.OTHER;
	}

	@Override
	public String getType() {
		return type;
	}

	@Override
	public String getInfoString() {
		return "<h1>Hard Grey Box</h1>" +
				"<p>" +
				"It's a box, it's grey, it's boring.<br>" +
				"But it's harder to kill then normal.</p>" +
				"<p><b>Damage Reduction:</b> " + CreepHardGrey.form.format(Math.round(reductionFactor * this.healthMax)) + "<br>" +
				"" + super.getInfoString() + "</p>";
	}

	@Override
	protected void doDamage(int user, long damage) {
		long realDamage = Math.round(damage - reductionFactor * this.healthMax);
		realDamage = Math.max(realDamage, 1);

		if (this.validTarget()) {
			this.health -= realDamage;
		}
		if (this.health <= 0) {
			this.validTarget = false;
			this.dead = true;
			ContextPlayer player = this.context.getPlayer(user);
			player.addScore(this.score);
			player.doReceive(this.price);
			this.context.enemyDied();
		}
		this.fireStatusChangedEvent();
	}

	@Override
	public void resetScale() {
		super.resetScale();
		Cache cache = cn.getCache();

		if (cache.hasShape("ESHGShape" + this.size.letter)) {
			this.bodyShape = cache.getShape("ESHGShape" + this.size.letter);
		} else {

			float scale = paintScale * this.size.scaleMod;
			float r = scale / 6;

			float[][] points = {
				{+r, +r},
				{+r, -r},
				{-r, -r},
				{-r, +r}
			};
			this.bodyShape = Geometry.coordsToGeneralPath(points, true);
			cache.putShape("ESHGShape" + this.size.letter, this.bodyShape);
		}
		this.bodyStroke = new BasicStroke(3.5f * strokeScale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
	}

	@Override
	public void paint(Graphics2D g2, int gameTime) {
		if (this.inactive || this.dead) {
			this.prevPaintTime = gameTime;
		} else {
			Stroke defaultStroke = g2.getStroke();
			this.prevPaintTime = gameTime;

			AffineTransform saveXform = g2.getTransform();
			g2.transform(atTranslate);

			if (this.shielded) {
				this.paintShield_bottom(g2);
			}
			this.paintHealthLine(g2);

			g2.setColor(this.bodyColor);
			g2.setStroke(this.bodyStroke);
			g2.draw(this.bodyShape);
			g2.setTransform(saveXform);

			if (this.shielded) {
				g2.transform(atTranslate);
				this.paintShield_top(g2);
				g2.setTransform(saveXform);
			}

			super.paintSelected(g2);

			g2.setStroke(defaultStroke);
		}
	}
}
